package cn.lutu.domain.service.impl;

import cn.lutu.domain.repository.RouteRepository;
import cn.lutu.domain.service.RouteService;
import cn.lutu.protocol.dto.RouteAddDTO;
import cn.lutu.protocol.dto.RouteDTO;
import cn.lutu.protocol.entity.Route;
import cn.lutu.protocol.entity.Spot;
import cn.lutu.protocol.vo.RouteVO;
import cn.lutu.protocol.vo.SpotVO;
import cn.lutu.protocol.vo.adminVO.AdminSpotVO;
import cn.lutu.protocol.vo.adminVO.RoleUpdateInfoVO;
import cn.lutu.util.exception.ServiceException;
import cn.lutu.util.pojo.vo.PageData;
import cn.lutu.util.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RouteServiceImpl  implements RouteService {

    @Value("${lutu.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    RouteRepository repository;
  //  @Override
//    public void insert(RouteDTO routeDTO) {
//        Route routes = new Route();
//        BeanUtils.copyProperties(routeDTO,routes);
//        routesMapper.insert(routes);
//    }
//
//    @Override
//    public void update(RouteDTO routeDTO) {
//        Route route = new Route();
//        BeanUtils.copyProperties(routeDTO,route);
//        routesMapper.update(route);
//    }
//
//    @Override
//    public void delete(Long id) {
//        routesMapper.delete(id);
//
//    }

    @Override
    public void insert(RouteDTO routeDTO) {

    }

    @Override
    public void update(RouteDTO routeDTO) {

    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public RouteVO select(Long id) {
        RouteVO routeVO = new RouteVO();
        return routeVO;
    }

    @Override
    public PageData<RouteVO> selectRouteList(Integer pageNum, Integer pageSize) {
        log.debug("正在处理获取酒店列表业务");
        PageData<RouteVO> pageData = repository.selectRouteList(pageNum, pageSize);
        return pageData;
    }

    @Override
    public PageData<RouteVO> selectRouteList(Integer pageNum) {
        log.debug("正在处理获取酒店列表业务");
        PageData<RouteVO> pageData = repository.selectRouteList(pageNum,defaultQueryPageSize);
        return pageData;
    }

    @Override
    public RouteVO selectRouteListById(Long id) {
        log.debug("根据ID查询路线数据，参数：{}",id);
        return repository.selectRouteListById(id);

    }

    @Override
    public List<RouteVO> selectRoute(Long id) {
        log.debug("根据ID查询路线产品价格，参数{}",id);
        return repository.selectRoute(id);
    }

    @Override
    public void addRoute(RouteAddDTO routeAddDTO) {
        log.info("开始执行新增路线的功能，名称为：{}",routeAddDTO);
        if (routeAddDTO == null) {
            String message = "您输入的数据为空!";
            log.debug(message);
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND,message);
        }
        Route route =new Route();
        BeanUtils.copyProperties(routeAddDTO,route);
        repository.addRoute(route);
    }

    @Override
    public void deleteRouteById(Long id) {
        log.info("开始执行根据ID删除路线的业务");
        RouteVO routeVO = repository.selectRouteListById(id);
        if (routeVO == null) {
            String message = "删除路线失败，尝试删除的数据不存在！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND, message);
        }

        int rows = repository.deleteById(id);
        if (rows != 1) {
            String message = "删除路线失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_UNKNOWN, message);
        }
    }

    @Override
    public Route selectRouteById(Long id) {
        log.debug("开始处理【根据ID查询路线】业务，参数：{}", id);
        Route route = repository.selectRouteById(id);
        if (route == null ) {
            String message = "获取路线详情失败，尝试访问的路线数据不存在！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND, message);
        }
        return route;
    }

    @Override
    public void updateRoute(RouteDTO routeDTO) {
        log.debug("开始处理【修改产品路线】的业务，参数：{}", routeDTO);

        Long id = routeDTO.getId();
        RouteVO routeVO = repository.getRouteById(id);
        if (routeVO == null) {
            String message = "修改路线失败，尝试修改的路线数据不存在！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_NOT_FOUND, message);
        }
        String name = routeDTO.getName();
        int count = repository.countByNameAndNotId(id, name);
        if (count > 0) {
            String message = "修改路线失败，角色名称已经被占用！";
            log.warn(message);
            throw new ServiceException(StatusCode.ERROR_UPDATE, message);
        }

        Route route = new Route();
        BeanUtils.copyProperties(routeDTO, route);
        int rows = repository.updateRoute(route);
        if (rows != 1) {
            String message = "修改路线失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(StatusCode.OPERATION_FAILED, message);
        }

    }

//    @Override
//    public List<VoteVO> selectVoteById(Long id) {
//        log.debug("根据ID查询门票数据，参数：{}",id);
//        List<VoteVO> list = repository.selectVoteById(id);
//        return list;
//    }


}
